<?php
/**
 * udalost_sabl Controller for Ohlasky Component
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * udalost_sabl Ohlasky Controller
 */
class OhlaskysControllerUdalost_sabl extends OhlaskysController {
/**
 * constructor (registers additional tasks to methods)
 * @return void
 */
    function __construct() {
        parent::__construct();

        // Register Extra tasks
        $this->registerTask( 'add'  , 	'edit' );
    }



    /**
     * display the edit form
     * @return void
     */
    function edit() {
        JRequest::setVar( 'view', 'udalost_sabl' );
        JRequest::setVar( 'layout', 'form'  );
        JRequest::setVar('hidemainmenu', 1);

        parent::display();
    }

    /**
     * save a record (and redirect to main page)
     * @return void
     */
    function save() {
        JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
        $table = JTable::getInstance('udalost_sabl','Table');
        $model = $this->getModel('udalost_sabl');
        $_data = $model->getData();
        $table->set('den_tydne', $_data->den_tydne);
        $cas = strval(JRequest::getVar('cas',  ''));
        $cas_konec = strval(JRequest::getVar('cas_konec', ''));
        $table->set('cas_konec', $cas_konec);
        $sablona_id = $_data->sablona_id;
        $table->set('cas', $cas);
        $table->set('sablona_id', $sablona_id);
        $table->set('misto_id', $_data->misto_id);
        if (!$table->check()) {
            $chyba = $table->getError();
            if (empty($chyba)) {
                $chyba = 'Chyba při uložení události!';
            }
            $msg = $chyba;
            $link = 'index.php?option=com_ohlasky&view=udalosti_sablony&sablona_id='.$sablona_id;
            $this->setRedirect($link, $msg, 'error');
        } else {
            if ($model->store($post)) {
                $_data = $model->getData();
                $id = $_data->sablona_id;
                $msg = JText::_( 'Událost byla uložena!' );
            } else {
                $msg = JText::_( 'Chyba při uložení události!' );
            }

            // Check the table in so it can be edited.... we are done with it anyway
            $link = 'index.php?option=com_ohlasky&view=udalosti_sablony&sablona_id='.$id;
            $this->setRedirect($link, $msg);
        }
    }

    /**
     * remove record(s)
     * @return void
     */
    function remove() {
        $model = $this->getModel('udalost_sabl');
        if(!$model->delete()) {
            $msg = JText::_( 'Chyba: Jedna nebo více událostí nemohly být vymazány!' );
        } else {
            $msg = JText::_( 'Událost byla vymazána' );
        }
        $sablona_id = strval(JRequest::getVar('sablona_id',  0));
        if (empty($sablona_id)) {
            $this->setRedirect( 'index.php?option=com_ohlasky', $msg );
        } else {
            $link = 'index.php?option=com_ohlasky&view=udalosti_sablony&sablona_id='.$sablona_id;
            $this->setRedirect($link, $msg);
        }
    }

    /**
     * cancel editing a record
     * @return void
     */
    function cancel() {
        $msg = JText::_( 'Operation Cancelled' );
        $this->setRedirect( 'index.php?option=com_ohlasky', $msg );
    }
}

